var clazz = {
		init : function() {
			clazz.clazzGrid = $('#clazzGrid');
			clazz.menu = $('#clazz-menu');
			clazz.clazzAddDialog = $('#clazzAddDialog');
			clazz.clazzAddForm = $('#clazzAddForm');
			clazz.clazzUpdateForm = $('#clazzUpdateForm');
			clazz.menuUpdate = $('#clazz-menuUpdate');
			clazz.clazzUpdateDialog = $('#clazzUpdateDialog');
			clazz.name=$('#clazzName');
			clazz.clazz=$('#clazz');
			clazz.clazzState=$('#clazz-state');
			
			
			clazz.clazzGrid.datagrid({
			    url:window.ctx+'/ClazzServlet?command=listClazz',
			    fixed: true,
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 20,
			    pageList: [10, 20, 30, 40, 50],
			    pageNumber: 1,
			    toolbar: '#clazzToolbar',
			    columns:[[
					{field:'id', width:100, checkbox: true},
					{field:'name', title:'班级', width:100,halign:undefined, align:'center'},
					{field:'course__text', title:'课程', halign:undefined,width:100, align:'center'},
					{field:'operateTime', title:'创建时间',halign:undefined, width:100, align:'center'},
					{field:'stateStr', title:'状态', width:100, halign:undefined,align:'center'}
					
			    ]],
				onDblClickRow: function(rowIndex, rowData) {
					clazz.toolbar.showUpdateDialog(rowData.id);
				},
				/* onBeforeLoad: function (param) {
					
					param.clazz=clazz.clazz.val();
			    }, */
				
			});
		},
		toolbar: {
			showAddDialog: function() {
				clazz.menu.combotree({
					url: window.ctx+'/ClazzServlet?command=listMenu',
					lines: true,
					multiple: true,
					checkbox: true,
					onlyLeafCheck: true,
					onLoadSuccess: function(node, data) {
						$(this).tree('expandAll');
						$(this).find('span.tree-checkbox').unbind().click(function () {
							var treeObj=clazz.menu.combotree('tree');
							treeObj.tree('select', $(this).parent());
		                    return false;
		                });
					},
					onSelect: function (node) {
						var treeObj=clazz.menu.combotree('tree');
		                var cknodes = treeObj.tree("getChecked");
		                for (var i = 0; i < cknodes.length; i++) {
		                    if (cknodes[i].id != node.id) {
		                    	treeObj.tree("uncheck", cknodes[i].target);
		                    }
		                }
		                if (node.checked) {
		                	treeObj.tree('uncheck', node.target);

		                } else {
		                	treeObj.tree('check', node.target);

		                }

		            }
				});
				clazz.clazzAddDialog.dialog('open');
				clazz.name.focus();
				// 坑，只能使用id选择器
			},
			hiddenAddDialog: function() {
				clazz.clazzAddDialog.dialog('close');
				clazz.toolbar.resetAddForm();
			},
			resetAddForm: function() {
				clazz.clazzAddForm.form('reset');
			},
			add: function() {
				if (clazz.clazzAddForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/ClazzServlet?command=addClazz', clazz.clazzAddForm.serialize(), function(data) {
						if (data==1) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							clazz.toolbar.resetAddForm();
							clazz.clazzGrid.datagrid('reload');
							
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
						clazz.toolbar.hiddenAddDialog();
					});
				}
			},
			showUpdateDialog: function(id) {
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/ClazzServlet?command=getclazz', {id : v}, function(data) {
						if (data) {
							clazz.clazzUpdateForm.form('load', {
								id: data.id,
								name: data.name,
								state:data.state
								
							});
							
							var checkedItems = [];
							if (data.courseId) {
								checkedItems = data.courseId.split(',');
							}
							clazz.menuUpdate.combotree({
								url: window.ctx+'/ClazzServlet?command=listMenu',
								lines: true,
								multiple: true,
								checkbox: true,
								onlyLeafCheck: true,
								onLoadSuccess: function(node, data) {
									var that = this;
									$(that).tree('expandAll');
									if (data) {
										$(data).each(function(index, value) {
											if ($.inArray(value.id + '', checkedItems) != -1) {
												$(that).tree('check', value.target);
											} else {
												$(that).tree('uncheck', value.target);
											}
										});
									}	
								}
							}); 
							$('#clazzUpdateDialog').dialog('open');
						} else {
							$.messager.alert('系统提示', '获取信息失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = clazz.clazzGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			hiddenUpdateDialog: function() {
				$('#clazzUpdateDialog').dialog('close');
				clazz.toolbar.resetUpdateFrom();
			},
			resetUpdateFrom: function() {
				clazz.clazzUpdateForm.form('reset');
			},
			update: function() {
				if (clazz.clazzUpdateForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/ClazzServlet?command=updateclazz', $("#clazzUpdateForm").serialize(), function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							clazz.toolbar.resetUpdateFrom();
							clazz.clazzGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
						clazz.toolbar.hiddenUpdateDialog();
					});
				}
			},
			updateClazzState: function() {
				var row = clazz.clazzGrid.datagrid('getSelections');
				if (row.length == 1) {
					var id;
					var state;
					$.each(row, function(i, v) {
						id=v.id;
						state=v.state;
					});
					$.post(window.ctx+'/ClazzServlet?command=updateClazzState', {id: id,state: state}, function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							clazz.clazzGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error')
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			},
			redo: function() {
				clazz.clazzGrid.datagrid('unselectAll');
			},
			search:function(){
				clazz.clazzGrid.datagrid('load',{
					clazz: clazz.clazz.val(),
					state:clazz.clazzState.val()
				});
				clazz.clazzGrid.datagrid('reload');
			}
			
		}
};

	$(function(){
		clazz.init();
	});
	
	
	var clazzName=$('#update-clazzName');
	$('#update-clazzName').blur(function(){
		$.post(window.ctx+'/ClazzServlet?command=checkClassName&name='+ clazzName.val() +'&time=' + new Date().getTime(), function(data){
		     if (data == 'false') { 
		    	msgShow('系统提示', '此班级名称非法或者已存在！', 'warning');
		     }
		}); 
	});